package cn.ai;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ChatTest {
    @Test
    public void test() throws Exception {
        StringBuilder s = new StringBuilder();
        
        URL url = new URL("http://localhost:8090/api/v1/ollama/generate_rag");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST"); // 确保使用正确的请求方法

        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        Map<String, String> inputs = new HashMap<>();
        inputs.put("ragTag", "明泽喜好");
        inputs.put("message", "明泽喜欢什么");
        inputs.put("model", "deepseek-r1:1.5b");


        try (OutputStream os = connection.getOutputStream()){
            byte[] bytes = JSON.toJSONString(inputs).getBytes(StandardCharsets.UTF_8);
            os.write(bytes, 0, bytes.length);
        }

        StringBuilder sb = new StringBuilder();
        try (Scanner scanner = new Scanner(connection.getInputStream(), StandardCharsets.UTF_8)) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                sb.append(line).append("\n");
            }
        }
        System.out.println(sb.toString());
    }
}
